let app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startX: 0,
    startY: 0,
    index:0,
    title:'2024临沂马拉松团报退费'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var open=wx.getStorageSync('open');
    var reg=/^[1-9]\d*$/;
      
    if(!reg.test(open.member_id)){
        app.showModal('您还没有绑定个人信息','add','#');
    }
    let d = app.globalData;
    this.setData({
  
        width:d.windowWidth,
        height:d.windowHeight,
        statusBarHeight:d.statusBarHeight,
        navHeight:d.navHeight,
        navWidth:d.navWidth,
        navRight:d.navRight,
        marginTop:d.navTitleTop,
        domain:d.domain
    });
   // //console.log(that.data.match_type)
  },
  goIndex(){
    console.log('goIndex');
    wx.reLaunch({
      url: '../mine/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getIndex();
  },
  goBack:function(){
      console.log('goBack');
    wx.navigateBack();
  },

  goto(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    var index=e.currentTarget.dataset.index;
    if(url!=undefined){
      if(index=='#'){
          wx.navigateTo({
            url: url,
          })
      }else{
          wx.reLaunch({
            url: url,
          })
      }
    }

},
 getIndex(){
     let that=this;
     var open=wx.getStorageSync('open');
     console.log(open);
     wx.request({
        url: app.globalData.domain+'member/fee/getRole',
        data: {open_id:open.id,member_id:open.member_id},
        success: function (res){
            var data=res.data;
          console.log(data);
          var role =data.role;
          var roleArray=[];
          roleArray.push('请选择交费金额');
          role.forEach(function (item,index){
            console.log(item);
            roleArray.push(item.name + ' ¥' +item.money)
          })
          var unit = data.unit;
          var unitArray=[];
          unitArray.push('请选择分队');
          unit.forEach(function (item,index){
            console.log(item);
            unitArray.push(item.name)
          })
          that.setData({
              role:data.role,
              roleArray:roleArray,
              member:data.member,
              yearArray:data.year,
              yearIndex:1,
              unitIndex:0,
              unit:data.unit,
              unitArray:unitArray
          })
        }
     })
 },
 bindRoleChange:function(e){
    var index=e.detail.value;
  
    this.setData({
    
      index:index
    })
  },
  bindYearChange:function(e){
    var index=e.detail.value;
  
    this.setData({    
      yearIndex:index
    })
  },
  bindUnitChange:function(e){
    var index=e.detail.value;
  
    this.setData({    
      unitIndex:index
    })
  },
  submitOrder(){
    let that=this;
    let data=this.data; 
    var index=data.index;
    var unitIndex=data.unitIndex;
    if(index==0){
      this.showModels('请选择交费金额','#','#')
    }else if(unitIndex==0){
        this.showModels('请选择分队','#','#')
    }else{
        var open = wx.getStorageSync('open');
        var member = data.member;
        var yearArray = data.yearArray;
        var role = data.role;
        var role_id = role[index-1].id
        var year = yearArray[data.yearIndex];
        var money = role[index-1].money;
       
        var unit=data.unit;
        var unit_id = unit[unitIndex-1].id;
       

    var msg='姓名：'+member.name+'\r\n交费年限：'+year+' \r\n交费金额：'+money;
 
    wx.showModal({
      title: '交费信息',
      content: msg,
      success(res) {
         if (res.confirm) {
           console.log('用户点击确定');
          // that.formSubmit();
          wx.request({
            // url: 'https://lypb.com/lupao/enrolls.php',
             url: app.globalData.domain+'member/fee/pay',
             header: {
               'Content-Type': 'application/x-www-form-urlencoded',
             },
             data: { 
                  money:money,
                  from:1, 
                  open_id:open.id,
                  openid:open.mini_openid, 
                  member_id: member.id,
                  unit_id:unit_id,
                  year:year,
                  role_id:role_id,
                  name:member.name

               },
             success:function(res){
                var data=res.data.data;
                var d = res.data;
                console.log(d)
               if(d.code==200){
                wx.requestPayment({
                    'timeStamp': data.timeStamp,
                    'nonceStr': data.nonceStr,
                    'package': data.package,
                    'signType': 'MD5',
                    'paySign': data.paySign,
                    'success': function (res) {
                    // console.log('success');
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 3000
                    });
                    setTimeout(function() {
                        //要延时执行的代码
                        wx.navigateTo({
                            url: '/pages/mine/index',
                        })
                        }, 1000) //延迟时间
                    //that.waitPayShow();
                    },
                    'fail': function (res) {
                    //console.log(res);
                    },
                    'complete': function (res) {
                    // console.log('complete');
                    }
                });
               }else{
                that.showModels(d.error,'#','#')
               }
               
             },
             fail:function(res){
   
             }
           })
         } else if (res.cancel) {
           console.log('用户点击取消')
         }
      }
   })
    
  }
},
showModels(msg,url,index){
    wx.showModal({
      title: '重要提示',
      content: msg,
      success(res) {
         if (res.confirm) {
           console.log('用户点击确定');
           if(url!='#'){
              if(index=='index'){
                wx.reLaunch({
                  url: url,
                })
              }else{
                wx.navigateTo({
          
                  url: url,
                })
              }
           }
          // that.formSubmit();
         } else if (res.cancel) {
           console.log('用户点击取消')
         }
      }
   })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})